package cn.wit.summit.chwan.dot;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;



import java.util.List;

import cn.wit.summit.chwan.utils.AppInfoUtil;
import cn.wit.summit.chwan.utils.LogUtils;

/**
 * Created by Alan on 2017/12/27.
 */

public class AlarmReceiver extends BroadcastReceiver {

    protected static final String TAG = "AlarmReceiver>>>:";

    private Context context;
    private String pakeName = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        pakeName = AppInfoUtil.getAppPkName(context);
//        LogUtils.e(TAG + pakeName);
//        if (isAppRunning())
//            context.startService(new Intent(context, DotService.class));
    }

    private boolean isAppRunning() {
        //判断应用是否在运行
        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
        boolean isAppRunning = false;
        if (pakeName.equals("")) return isAppRunning;
        for (ActivityManager.RunningTaskInfo info : list) {
            if (info.topActivity.getPackageName().equals(pakeName)
                    || info.baseActivity.getPackageName().equals(pakeName)) {
                isAppRunning = true;
                break;
            }
        }
        LogUtils.e(TAG + "isAppRunning:" + isAppRunning);
        return isAppRunning;
    }

}